define(['jquery'], function (jQuery) {
// 轮询器
    var pollers = {
        start: function (url, data, callable) {
            pollers.step = 0;
            pollers.total = 0;
            pollers.progressbar_value = 0;
            pollers.url = url;
            pollers.data = data;
            pollers.callable = callable;
            pollers.polling();
        },
        proceed: function () {
            // 继续功能
            pollers.step++;
            pollers.polling();
        },
        polling: function () {
            // 合并form序列化数组数据
            var request_data = {};
            for(var k = 0,len=pollers.data.length; k < len; k++) {
                request_data[pollers.data[k].name] = pollers.data[k].value;
            }
            request_data.step = pollers.step;

            // Ajax请求轮询
            jQuery.ajax({
                url: pollers.url,
                type: 'POST',
                async: true, //或false,是否异步
                data: request_data,
                timeout: 5000, //超时时间
                dataType: 'json', //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend: function (xhr) {
                    pollers.nullMethod(xhr);
                },
                success: function (res, textStatus, jqXHR) {
                    //  成功后加载内容
                    if (parseInt(res.code) === 1) {
                        pollers.step++;
                        pollers.total = res.total;

                        // 心跳调用
                        setTimeout(function () {
                            pollers.polling();
                        }, 100);
                    }
                    else if(parseInt(res.code) === 0){
                        pollers.nullMethod('over');
                    }
                    else{
                        console.log("状态错误，请检查！");
                        console.log(textStatus);
                        console.log(jqXHR);
                    }

                    // 函数回调
                    if("function" === typeof pollers.callable){
                        pollers.callable(res);
                    }
                },
                error: function (xhr, textStatus) {
                    console.log(xhr);
                    console.log(textStatus);
                },
                complete: function () {
                }
            });
        },
        nullMethod: function (){}
    };

    return pollers;

});